﻿using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syngine;
using Syngine.Physics;
using Syngine.Physics.Components;

namespace BenchMark.CarGame
{
	/// <summary>
	/// 
	/// </summary>
	public class Vehicle : DrawableSprite
	{
		private Wheel _frontWheel;
		private Wheel _rearWheel;

		public override void Initialize()
		{
			_frontWheel = new Wheel();
			_rearWheel = new Wheel();

			//JointFactory.CreateRevoluteJoint()

			Add(_frontWheel);
			Add(_rearWheel);

			base.Initialize();
		}

		public override void LoadContent(LoadContext context)
		{
			base.LoadContent(context);

			var data = new uint[Texture.Width() * Texture.Height()];
			Texture.GetData(data);
			var verts = PolygonTools.CreatePolygon(data, Texture.Width(), true);
			var scale = new Vector2(0.07f, 0.07f);
			verts.Scale(ref scale);
			var list = BayazitDecomposer.ConvexPartition(verts);
			Body = BodyFactory.CreateCompoundPolygon(WorldContext.World, list, 1);
			Body.BodyType = BodyType.Dynamic;
		}

		protected override Texture2D GetAsset(LoadContext context)
		{
			return context.Load<Texture2D>("CarBody");
		}

		protected override void UpdateCore(UpdateContext context)
		{

			base.UpdateCore(context);
		}

		protected override void DrawCore(DrawContext context)
		{

			base.DrawCore(context);
		}
	}
}
